package com.google.gwt.sample.dynatablerf.client.presenters;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.TabLayoutPanel;

public abstract class AbstractTabPresenter extends AbstractPresenter {

	public interface TabDisplay extends IsWidget {
		IsWidget getTabHeader();
	}

	private TabLayoutPanel tabLayoutPanel;

	public AbstractTabPresenter(EventBus eventBus) {
		super(eventBus);
	}

	public void go(final TabLayoutPanel tabLayoutPanel) {
		this.tabLayoutPanel = tabLayoutPanel;

		Scheduler.get().scheduleDeferred(new ScheduledCommand() {

			@Override
			public void execute() {
				bind();
				tabLayoutPanel.add(getDisplay(), getDisplayTabHeader());
				tabLayoutPanel.selectTab(getDisplay().asWidget());
			}
		});

	}

	protected IsWidget getDisplayTabHeader() {
		return getTabDisplay().getTabHeader();
	}

	protected abstract TabDisplay getTabDisplay();

	protected TabLayoutPanel getTabLayoutPanel() {
		return tabLayoutPanel;
	}

	@Override
	protected IsWidget getDisplay() {
		return getTabDisplay();
	}

}
